package com.quanfaqiang.demo1.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.mapper.NoticeMapper;
import com.quanfaqiang.demo1.mapper.UserMapper;
import com.quanfaqiang.demo1.pojo.*;
import com.quanfaqiang.demo1.service.NoticeService;
import com.quanfaqiang.demo1.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Notice notice) {
        Map<String, Object> map = ThreadLocalUtil.get();
        System.out.println(map);
        String username = (String) map.get("username");
        Integer id = (Integer) map.get("id");
        notice.setUserid(id);
        noticeMapper.insert(notice);
    }

    @Override
    public void updateById(Notice notice) {
        notice.setUpdateTime(LocalDateTime.now());
        noticeMapper.updateById(notice);
    }

    @Override
    public void removeById(Integer id) {
        noticeMapper.deleteById(id);
    }

    @Override
    public void removeBatchByIds(List<Integer> ids) {
        noticeMapper.deleteBatchIds(ids);
    }

    @Override
    public Page<Notice> selectAll(int currentPage, int pageSize) {
        Page<Notice> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Notice> QueryWrapper = new QueryWrapper<>();
        Page<Notice> noticePage = noticeMapper.selectPage(page, QueryWrapper);
        List<Notice> records = noticePage.getRecords();
        for (Notice record : records) {
            User user = userMapper.selectById(record.getUserid());
            record.setUsername(user.getUsername());
        }
        return noticePage;
    }

    @Override
    public Page<Notice> serch(Integer currentPage, Integer pageSize, String username, String title, LocalDateTime startTime, LocalDateTime endTime, Integer state) {
        Page<Notice> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();

        if (title != null && !"".equals(title)) {
            noticeQueryWrapper.like("title", title);
        }
        if (state != null && !"".equals(state)) {
            noticeQueryWrapper.eq("state", state);
        }
        if (username != null && !"".equals(username)) {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
            if (user != null) {
                noticeQueryWrapper.eq("userid", user.getId());
            }
        }
        if (startTime != null && !"".equals(startTime)) {
            noticeQueryWrapper.ge("create_time", startTime);
        }
        if (endTime != null && !"".equals(endTime)) {
            noticeQueryWrapper.le("update_time", endTime);
        }
        Page<Notice> noticePage = noticeMapper.selectPage(page, noticeQueryWrapper);
        List<Notice> records = noticePage.getRecords();
        for (Notice record : records) {
            User user = userMapper.selectById(record.getUserid());
            record.setUsername(user.getUsername());
        }
        System.out.println(noticePage.getRecords()+"====================");
        return noticePage;
    }
}
